package cn.icare.cms.impl;

import cn.icare.cms.ListUtil;
import cn.icare.cms.MenuService;
import cn.icare.cms.dao.mapper.MenuMapper;
import cn.icare.cms.dao.mapper.UserPositionMenuMapper;
import cn.icare.cms.dao.po.Menu;
import cn.icare.cms.dao.po.UserPositionMenu;
import cn.icare.cms.model.vo.rest.MenuVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenuMapper menuMapper;
    @Resource
    private UserPositionMenuMapper userPositionMenuMapper;
    private final Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);

    @Override
    public boolean updateUserPositionMenu(Integer positionId, List<Integer> menuIds) {
        boolean result = false;
        try {
            if (positionId == null) {
                this.logger.warn("缺少positionId参数");
                return result;
            }

            Example example = new Example(UserPositionMenu.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("positionid", positionId);
            this.userPositionMenuMapper.deleteByExample(example);
            if (ListUtil.isNotEmpty(menuIds)) {
                for (Integer menuId : menuIds) {
                    UserPositionMenu menuUserGroupDB = new UserPositionMenu();
                    menuUserGroupDB.setMenuid(menuId);
                    menuUserGroupDB.setPositionid(positionId);
                    this.userPositionMenuMapper.insert(menuUserGroupDB);
                }
            }
            result = true;
        } catch (Exception e) {
            this.logger.error("updateUserPositionMenu 异常", e);
        }
        return result;
    }

    @Override
    public MenuVO getOne(Integer id) {
        MenuVO result = null;
        try {
            Menu menuDB = this.menuMapper.selectByPrimaryKey(id);
            result = this.toVO(menuDB);
        } catch (Exception e) {
            this.logger.error("getOne 异常", e);
        }
        return result;
    }

    @Override
    public List<Integer> getList(Integer positionId) {
        List<Integer> result = new ArrayList<Integer>();
        try {
            List<Integer> tempMenuIdList = new ArrayList<Integer>();
            if (positionId != null) {
                if(positionId==1)
                {
                 //系统管理员
                 List<Menu> menus=this.menuMapper.selectAll();
                    if (ListUtil.isNotEmpty(menus)) {
                        for (Menu menu : menus) {
                            result.add(menu.getId());
                        }
                    }
                }
                else {
                    Example example = new Example(UserPositionMenu.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("positionid", positionId);
                    List<UserPositionMenu> menuUserGroupDBList = this.userPositionMenuMapper.selectByExample(example);
                    if (ListUtil.isNotEmpty(menuUserGroupDBList)) {
                        for (UserPositionMenu menuUserGroupDB : menuUserGroupDBList) {
                            result.add(menuUserGroupDB.getMenuid());
                        }
                    }
                }
            }


        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }

    @Override
    public List<Menu> getAllList() {
       return this.menuMapper.selectAll();
    }

    @Override
    public List<Menu> getPositionMenuList(Integer positionId) {

        List<Menu> result = new ArrayList<>();
        List<Integer> menuIds=this.getList(positionId);
        if(menuIds.size()>0) {
            Example example = new Example(Menu.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("id", menuIds);
            result.clear();
            result.addAll(this.menuMapper.selectByExample(example));
        }

        return result;
    }


    private MenuVO toVO(Menu menu) {
        MenuVO result = null;
        try {
            if (menu != null) {
                result = new MenuVO();
                result.setActionUrl(menu.getActionurl());
             //   result.setIcon(menu.getIcon());
                result.setId(menu.getId());
                result.setMenuName(menu.getName());
                result.setParentId(menu.getParentid());
            }
        } catch (Exception e) {
            this.logger.error("toVO 异常", e);
        }
        return result;
    }
}
